package com.dudy;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * @program: zhuguangDemo
 * @description: ${description}
 * @author: dudy
 * @create: 2018-04-02 19:34
 **/
public class PipedTransfer {
    private static class Print implements Runnable {
        PipedReader in;

        public Print(PipedReader in) {
            this.in = in;
        }

        @Override
        public void run() {
            int receive = 0;
            try {
                while ((receive = in.read()) != -1) {
                    System.out.println((char) receive);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        PipedReader pipedReader = new PipedReader();
        PipedWriter pipedWriter = new PipedWriter();
        try {
            pipedWriter.connect(pipedReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Thread thread = new Thread(new Print(pipedReader), "PrintThread");
        thread.start();
        int recevie = 0;
        try {
            while ((recevie = System.in.read()) != -1) {
                pipedWriter.write(recevie);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                pipedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
